package com.javalong.affairs.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.javalong.affairs.R;
import com.javalong.affairs.adapter.UserAdapter;
import com.javalong.affairs.constant.BundleKey;
import com.javalong.affairs.constant.DataType;
import com.javalong.affairs.constant.MapKey;
import com.javalong.affairs.constant.RequestKey;
import com.javalong.affairs.data.AllData;
import com.javalong.affairs.manager.IntentManager;
import com.javalong.affairs.model.User;
import com.javalong.affairs.ui.SimplePullRefreshList;
import com.javalong.affairs.ui.TopTitle;
import com.javalong.affairs.ui.TopTitle.OnRightTextClick;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_userlist)
public class UserListActivity extends BaseActivity implements OnRightTextClick,
		OnItemClickListener {
	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.lv_user)
	private SimplePullRefreshList lv_user;
	private UserAdapter adapter;
	private int deptId = 0;
	private List<User> userList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		topTitle.setOnLeftDrawableClick(this);
		userList = AllData.GetInstance().appendListData(
				R.string.data_dept_users, new ArrayList<User>());
		adapter = new UserAdapter(this, userList);
		lv_user.setAdapter(adapter);
		lv_user.initData(getMapData());
		lv_user.refresh();
		topTitle.setOnRightTextClick(this);
		lv_user.setOnItemClickListener(this);
	}

	private Map<String, Object> getMapData() {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put(MapKey.ACTION_NAME, "User");
		param.put(MapKey.METHOD_NAME, "outUserListBySection");
		param.put(MapKey.DATA_RESID, R.string.data_dept_users);
		param.put(MapKey.DATA_TYPE, DataType.LIST_USER);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(RequestKey.ID, deptId);
		param.put(MapKey.REQUEST_PARAMS, params);
		return param;
	}

	@Override
	public void rightTextClick() {
		Intent intent = new Intent();
		intent.putExtra(BundleKey.USERLIST,
				(Serializable) adapter.getSelectedUsers());
		setResult(RESULT_OK, intent);
		finish();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Bundle bundle = new Bundle();
		bundle.putString(BundleKey.USERID, userList.get(position).id);
		IntentManager.doActivity2(this, ScheduleListActivity.class, bundle);
	}
}
